﻿
Imports System.IO
Imports webctrl

Public Class antiInputSpam(Of RETIRE_SECONDS As _int64, INTERVAL_LIMIT_SECONDS As _int64, BYPASS_INTERNAL_REQUEST As _boolean)
    Inherits webctrl.antiInputSpam(Of RETIRE_SECONDS, INTERVAL_LIMIT_SECONDS)

    Public Const defaultQuery As String = "cs"
    Private Shared bypassInternalRequest As Boolean = True

    Shared Sub New()
        bypassInternalRequest = +(alloc(Of BYPASS_INTERNAL_REQUEST)())
    End Sub

    Private Shared Function context() As httpContext
        Return httpServer3.currentContext()
    End Function

    Private Shared Function defaultBuff() As Stream
        Return context().buff()
    End Function

    Public Shared Shadows Function checkIpTable(ByVal context As httpContext, ByVal query As String) As Boolean
        If context Is Nothing OrElse isemptystring(query) Then
            Return False
        Else
            Return webctrl.antiInputSpam(Of RETIRE_SECONDS, INTERVAL_LIMIT_SECONDS). _
                       checkIpTable(context.requestIp(), context.url().query(query)) OrElse _
                   (bypassInternalRequest AndAlso context.isinternalRequest())
        End If
    End Function

    Public Shared Shadows Function checkIpTable(ByVal query As String) As Boolean
        Return checkIpTable(context(), query)
    End Function

    Public Shared Shadows Function checkIpTable(ByVal context As httpContext) As Boolean
        Return checkIpTable(context, defaultQuery)
    End Function

    Public Shared Shadows Function checkIpTable() As Boolean
        Return checkIpTable(context(), defaultQuery)
    End Function

    Public Sub New(ByVal page As xhtmlpage,
                   ByVal query As String, _
                   ByVal buff As Stream, _
                   ByRef cs As String)
        MyBase.New(page, context().requestIp(), query, buff, cs)
    End Sub

    Public Sub New(ByVal page As xhtmlpage, ByVal query As String, ByRef cs As String)
        Me.New(page, query, defaultBuff(), cs)
    End Sub

    Public Sub New(ByVal page As xhtmlpage, ByVal buff As Stream, ByRef cs As String)
        Me.new(page, defaultQuery, buff, cs)
    End Sub

    Public Sub New(ByVal page As xhtmlpage, ByRef cs As String)
        Me.New(page, defaultQuery, defaultBuff(), cs)
    End Sub

    Public Sub New(Optional ByVal page As xhtmlpage = Nothing)
        Me.New(page, defaultQuery, defaultBuff(), Nothing)
    End Sub
End Class

Public Class antiInputSpam
    Inherits antiInputSpam(Of _3600, _10, _true)

    Public Sub New(ByVal page As xhtmlpage,
                   ByVal query As String, _
                   ByVal buff As Stream, _
                   ByRef cs As String)
        MyBase.New(page, query, buff, cs)
    End Sub

    Public Sub New(ByVal page As xhtmlpage,
                   ByVal query As String, _
                   ByRef cs As String)
        MyBase.New(page, query, cs)
    End Sub

    Public Sub New(ByVal page As xhtmlpage, ByVal buff As Stream, ByRef cs As String)
        MyBase.new(page, buff, cs)
    End Sub

    Public Sub New(ByVal page As xhtmlpage, ByRef cs As String)
        MyBase.new(page, cs)
    End Sub

    Public Sub New(Optional ByVal page As xhtmlpage = Nothing)
        MyBase.New(page)
    End Sub
End Class
